<?php
/**
 * Created by PhpStorm.
 * User: jurentech
 * Date: 2019-10-09
 * Time: 17:30
 */
namespace app\master\controller;

class Counpon extends Base
{
    public function index()
    {
        return $this->fetch();
    }
    public function getlist()
    {
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('coupon')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $k=>$v){
            $result['data'][$k]['send_start_time']=date('Y-m-d H:i:s', $result['data'][$k]['send_start_time']);
            $result['data'][$k]['send_end_time']=date('Y-m-d H:i:s', $result['data'][$k]['send_end_time']);
            $result['data'][$k]['use_start_time']=date('Y-m-d H:i:s', $result['data'][$k]['use_start_time']);
            $result['data'][$k]['use_end_time']=date('Y-m-d H:i:s', $result['data'][$k]['use_end_time']);

        }
        $result['count'] = db('coupon')->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }
    public function addchekou()
    {
        if(request()->isPost()){
            $data=input('post.');
            $coupon['add_time']=time();
            $coupon['coupon_type']=1;
            $coupon['status']=1;
            $coupon['name']=$data['name'];
            $coupon['createnum']=$data['createnum'];
            $coupon['use_type']=$data['use_type'];
            $coupon['condition']=$data['condition'];
            $coupon['money']=$data['money'];
            $coupon['send_start_time']=strtotime($data['send_start_time']);
            $coupon['send_end_time']=strtotime($data['send_end_time']);
            $coupon['use_start_time']=strtotime($data['use_start_time']);
            $coupon['use_end_time']=strtotime($data['use_end_time']);
            $coupon_id=db('coupon')->insertGetId($coupon);
            $coupon_goods['coupon_id']=$coupon_id;
            $coupon_goods['use_type']=$coupon['use_type'];
            if($coupon_goods['use_type']==1){
                $coupon_goods['goods_id']=$data['goods_id'];
            }
            if($coupon_goods['use_type']==2){
                $coupon_goods['category_id']=$data['category_id'];
            }
            db('coupon_goods')->insert($coupon_goods);
            return $this->_ajax_return(200,'添加成功','index');

        }
        $data = db('goods_category')->where(['isdelete' => 0])->select();
        $goods = db('goods')->where(['isdelete' => 0])->select();
        $this->assign('category', $data);
        $this->assign('goods',$goods);
        return $this->fetch();
    }
    public function addmanjian()
    {
        if(request()->isPost()){
            $data=input('post.');
            $coupon['add_time']=time();
            $coupon['coupon_type']=0;
            $coupon['status']=1;
            $coupon['name']=$data['name'];
            $coupon['createnum']=$data['createnum'];
            $coupon['use_type']=$data['use_type'];
            $coupon['condition']=$data['condition'];
            $coupon['money']=$data['money'];
            $coupon['send_start_time']=strtotime($data['send_start_time']);
            $coupon['send_end_time']=strtotime($data['send_end_time']);
            $coupon['use_start_time']=strtotime($data['use_start_time']);
            $coupon['use_end_time']=strtotime($data['use_end_time']);
            $coupon_id=db('coupon')->insertGetId($coupon);
            $coupon_goods['coupon_id']=$coupon_id;
            $coupon_goods['use_type']=$coupon['use_type'];
            if($coupon_goods['use_type']==1){
                $coupon_goods['goods_id']=$data['goods_id'];
            }
            if($coupon_goods['use_type']==2){
                $coupon_goods['category_id']=$data['category_id'];
            }
            db('coupon_goods')->insert($coupon_goods);
            return $this->_ajax_return(200,'添加成功','index');

        }
        $data = db('goods_category')->where(['isdelete' => 0])->select();
        $goods = db('goods')->where(['isdelete' => 0])->select();
        $this->assign('category', $data);
        $this->assign('goods',$goods);
        return $this->fetch();
    }
}